package models;

import java.util.*;
import javax.persistence.*;
import java.text.SimpleDateFormat;

import play.Logger;
import play.data.format.*;
import play.data.validation.*;

import models.*;

public class MovieSessionCreateForm {	
	
	@Constraints.Required
	public Long movie_id;	
	
	@Constraints.Required
	@Formats.NonEmpty
	public Float price;
	
	@Constraints.Required
	@Formats.NonEmpty
	public int house_id;
	
	@Constraints.Required
	@Formats.NonEmpty
	public Long movieLang;
	
	public String lang;

	@Constraints.Required
	@Formats.NonEmpty
	public String showtime;    

	public String validate() {
		Movie movie = Movie.findById(movie_id, "en");
		if (movie == null)
			return "Invalid movie ID";
			
		int duration = movie.duration.intValue();
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
		Date p_showtime;
		try{
			p_showtime = sdf.parse(showtime);
		}catch(Exception e){
			return "Invalid showtime";
		}
		
		Calendar c = Calendar.getInstance();
		c.setTime(p_showtime);
		c.add(Calendar.MINUTE, duration);
		Date showtime_end = c.getTime();	
		
		if(View_MovieSessionStartEnd.isTimeSlotOccupied(house_id, p_showtime,showtime_end))
			return "The time slot is already occupied";
		
		return null;
	}


}

